package in.muton.edushare.grading.panels.studentGrades;

import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.grading.panels.gradeDisplay.ItemView;
import in.muton.edushare.grading.panels.studentGrades.GradingUtil.CourseGradeHelper;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.SimpleList;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.dao.academics.grades.InternalGrades;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Users;

public class StudentCourseEdit extends EdusharePanel {
	private StudentMainGradePanel panel = null;
	private IModel<CourseInstances> cInst;
	private boolean initialized = false;
	private List<CourseGradeHelper> cghList = new ArrayList<CourseGradeHelper>();
	private static final long serialVersionUID = -4717692894189631073L;

	public StudentCourseEdit(String id, EduShareBasePage page,
			final IModel<CourseInstances> cInst,
			final StudentMainGradePanel panel) {
		super(id, page);
		this.cInst = cInst;
		this.panel = panel;
		add(new Label("courseName", cInst.getObject().getCourse()
				.getCourseName()));
		Form<Void> f;
		add(f = new Form<Void>("gradeEdit") {
			@Override
			public void onSubmit() {
				GradingUtil util = new GradingUtil(panel.getcInstModel());
				for (CourseGradeHelper cgh : cghList) {
					if (cgh.isLetterGrade) {
						if (!util.isValidKey(cgh.temp)) {
							setError(cgh.temp + " for student "
									+ cgh.user.getObject()
											.getCompleteUserName()
									+ " is not a valid grade.");
							return;
						}
						util.saveMarks(cgh);
					} else {
						try {
							if (!util.isMarksOK(Double.parseDouble(cgh.temp))) {
								setError("Marks for students must be between 0 and 100.");
								return;
							}
							util.saveMarks(cgh);
						} catch (NumberFormatException e) {
							setError("Marks for students must be valid numbers between 0 and 100");
						}
					}
				}
				setViewMode();
				initialized = false;
			}

			private static final long serialVersionUID = -5897305251795054935L;
		});

		f.add(new Button("gcCancel") {
			@Override
			public void onSubmit() {
				setViewMode();
			}

			private static final long serialVersionUID = -3582078064409114369L;
		}.setDefaultFormProcessing(false));

		f.add(new Button("gcCalculate") {
			@Override
			public void onSubmit() {
				InternalGrades ig = new InternalGrades(getORMInterface());
				CourseInstances cinst = panel.getcInstModel().getObject();
				List<Users> uL = new CourseDataHandler(panel.getcInstModel())
						.getStudents().getUtilList();
				for (Users u : uL) {
					ig.recalculateCGPA(cinst, u);
				}
				setViewMode();
				initialized = false;
			}

			private static final long serialVersionUID = -9086594054728012768L;
		}.setDefaultFormProcessing(false));

		f.add(new Button("reset") {
			private static final long serialVersionUID = 7119945959690789115L;

			@Override
			public void onSubmit() {
				InternalGrades ig = new InternalGrades(getORMInterface());
				CourseInstances cinst = panel.getcInstModel().getObject();
				List<Users> uL = new CourseDataHandler(panel.getcInstModel())
						.getStudents().getUtilList();
				for (Users u : uL) {
					ig.removeCourseGrade(cinst, u);
				}
				initialized = false;
			}
		}.setDefaultFormProcessing(false));

		f.add(new ListView<CourseGradeHelper>("studentGradesLV",
				new SimpleList<CourseGradeHelper>() {
					private static final long serialVersionUID = 1L;

					public List<CourseGradeHelper> getObject() {
						return getCGHelper();
					}
				}) {
			private static final long serialVersionUID = 1024437957325949576L;

			@Override
			protected void populateItem(ListItem<CourseGradeHelper> arg0) {
				CourseGradeHelper cgh = arg0.getModelObject();
				StudentMainGradePanel.log.debug("cgh.user "+cgh.user.getObject().getCompleteUserName());
				StudentMainGradePanel.log.debug("cgh.temp "+cgh.temp);
				boolean isVisible = StudentItemView.isStudentInSearch(cgh.user
						.getObject(), panel.getStudentName());
				arg0.setVisible(isVisible);
				arg0.add(new EduShareURLPanel("user", cgh.user));
				arg0.add(new TextField<String>("grade",
						new PropertyModel<String>(cgh, "temp")));
				arg0.add(new ItemView("itemView", cgh.user, cInst));
			}
		});
	}

	private List<CourseGradeHelper> getCGHelper() {
		if (!initialized) {
			initialized = true;
			cghList = new GradingUtil(panel.getcInstModel()).getGrades();
		}
		return cghList;
	}

	@Override
	public boolean isVisible() {
		CourseRoleCheck crc = new CourseRoleCheck(cInst.getObject(),
				getLoggedInUser());
		return panel.getActiveMode() == StudentMainGradePanel.EDIT_COURSE_GRADES
				&& (crc.isTeacher() || crc.isAdmin());
	}

	public void setViewMode() {
		panel.setActiveMode(StudentMainGradePanel.VIEW_COURSE_GRADES);
	}

	public void setInitialized(boolean initialized) {
		this.initialized = initialized;
	}
}
